Hi, I was anticipating if someone could give me some constructive feedback on the code below (not something like "don't use pointers" or "always use braces with loops or conditions", more like, "there is problem in the algorithm", or "this could be a better approach/algorithm", or "error in the code"). No offense to anyone. I'm here to learn. Thanks.
Code:
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void reversearray(int* arr, int size)
{
for (int i = 0; i < size / 2; i++) swap(arr + i, arr + ((size - 1) - i));
}
int partitionarray(int* arr, int start, int end)
{
int pivot = arr[end];
int pIndex = start;
for (int i = start; i < end; i++)
{
if (arr[i] <= pivot) swap(&arr[i], &arr[pIndex++]);
}
swap(&arr[pIndex], &arr[end]);
return pIndex;
}
void quicksort(int* arr, int start, int end, _Bool reverse)
{
if (start < end)
{
int pIndex = start;
while (pIndex < end) pIndex = partitionarray(arr, start, pIndex + 1);
if (reverse) reversearray(arr, end + 1);
}
}
int main()
{
int arr[] = { 8, 5, 9, 0, 6, 4, 3, 7, 1, 2 };
int size = sizeof(arr) / sizeof(arr[0]);
quicksort(arr, 0, size - 1, 1);
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
return 0;
}